package com.haochang.chunkvod.controller.adapter.order;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.base.OnBaseLongClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.database.order.RoomInfoDao;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.controller.activity.hostess.ConsumeInfoActivity;
import com.haochang.chunkvod.controller.activity.order.CustomActivity;
import com.haochang.chunkvod.controller.activity.order.OrderMainActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.BaseInfo;
import com.haochang.chunkvod.model.order.RoomInfo;
import com.haochang.chunkvod.tools.vod.other.DrawableUtils;
import com.haochang.chunkvod.view.vod.activity.CustomDialogActivity;
import com.haochang.chunkvod.view.vod.activity.SingleInputActivity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

/**
 * 自定义适配器
 */
public class CustomAdapter extends QuickAdapter
{
	private Context mContext;
	private List<BaseInfo> baseInfoList;

	public CustomAdapter(Context context, int layoutResId, List data)
	{
		super(context, layoutResId, data);
		mContext = context;
	}

	public void setBaseInfoList(List<BaseInfo> baseInfoList)
	{
		this.baseInfoList = baseInfoList;
	}

	@Override
	protected void convert(BaseAdapterHelper helper, final Object object)
	{
		final RoomInfo item = (RoomInfo) object;
		final BaseTextView tv_roomStatus = helper.getView(R.id.tv_roomStatus);
		helper.setText(R.id.tv_room_type_count, item.getRoomType() + "(" + item.getRoomPeopleCount() + ")");
		helper.setText(R.id.tv_roomNum, item.getRoomNum());
		helper.setText(R.id.tv_time, TextUtils.isEmpty(item.getStart_time()) ? TimeUtils.getMinutesString(item.getEnd_time())
				: TimeUtils.getMinutesString(item.getStart_time()) + "-" + TimeUtils.getMinutesString(item.getEnd_time()));
		helper.setText(R.id.tv_remark, item.getRoomRemark());
		if (item.getOrderNum() > 0)
			helper.getView(R.id.img_shopping).setVisibility(View.VISIBLE);
		else
			helper.getView(R.id.img_shopping).setVisibility(View.GONE);
		if (item.getType() == 0)
		{
			helper.getView(R.id.tv_batch_num).setVisibility(View.GONE);//隐藏批次号
			if (CollectionUtils.isEmpty(baseInfoList))
				DrawableUtils.setDefaultRoomStyle(mContext, item.getRoomStatus(), item.getUsage(), helper.getView(), tv_roomStatus);
			else
				DrawableUtils.setRoomStyleByService(mContext, item.getRoomStatus(), item.getUsage(), helper.getView(), tv_roomStatus, baseInfoList);
		} else
		{
			if (item.getAbs_number() == 0)
			{
				helper.getView(R.id.tv_batch_num).setVisibility(View.GONE);
			} else
			{
				helper.getView(R.id.tv_batch_num).setVisibility(View.VISIBLE);
				helper.setText(R.id.tv_batch_num, "(+" + item.getAbs_number() + ")");
			}
			if (CollectionUtils.isEmpty(baseInfoList))
				DrawableUtils.settDefaultDeskStyle(mContext, item.getRoomStatus(), helper.getView(), tv_roomStatus);
			else
				DrawableUtils.setDeskStyleByService(mContext, item.getRoomStatus(), helper.getView(), tv_roomStatus, baseInfoList);
		}
		helper.getView(R.id.list_layout).setTag(R.id.order_home_page_list, item);
		helper.getView(R.id.list_layout).setOnClickListener(onBaseClickListener);
		helper.getView(R.id.list_layout).setOnLongClickListener(onBaseLongClickListener);
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.list_layout:
				RoomInfo item = (RoomInfo) v.getTag(R.id.order_home_page_list);
				if (item.getType() == 0)
				{
					if (!TextUtils.isEmpty(item.getBillId()) && !TextUtils.isEmpty(item.getBillStatus())
							&& (item.getBillStatus().equals("unpaied") || item.getBillStatus().equals("paied")))
					{
						onOrderClick(item);
					} else
						ToastUtils.makeText(mContext, R.string.no_order, ToastUtils.TOAST_TIME).show();
				} else
				{
					onOrderClick(item);
				}
				break;
			default:
				break;
			}
		}
	};

	private OnBaseLongClickListener onBaseLongClickListener = new OnBaseLongClickListener()
	{
		@Override
		public boolean onBaseLongClick(View v)
		{
			final RoomInfo item = (RoomInfo) v.getTag(R.id.order_home_page_list);
			List<String> mMoreOptionItemList = new ArrayList<String>();
			mMoreOptionItemList.add("备注");
			if (item.isCustom())
				mMoreOptionItemList.add("从自定义移除");
			else
				mMoreOptionItemList.add("添加到自定义");
			mMoreOptionItemList.add("修改人数");

			new VodDialog.Builder(context).dialogEnum(VodDialog.DialogEnum.LIST).ListTitleName(item.getRoomNum())
					.onDialogListListener(new VodDialog.Builder.OnDialogListListener()
					{
						@Override
						public void onSelectItem(int position)
						{
							switch (position)
							{
							case 1:
								onRemarkClick(item);
								break;
							case 2:
								if (item.isCustom())
								{
									RoomInfoDao.getDao().delete(RoomInfo.class, item);
									remove(item);
								}
								break;
							case 3:
								onPeopleCountClick(item);
								break;
							default:
								break;
							}
						}
					}).dialogList(mMoreOptionItemList).cancelable(true).build().show();
			return false;
		}
	};

	/**
	 * 点击进入点餐主页
	 * @param roomInfo
	 */
	private void onOrderClick(RoomInfo roomInfo)
	{
		Intent intent = new Intent(mContext, OrderMainActivity.class);
		intent.putExtra(IntentKey.ROOM_NUM, roomInfo.getRoomNum());
		intent.putExtra(IntentKey.ROOM_ID, roomInfo.getRoomId());
		intent.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
		intent.putExtra(IntentKey.CHARGING_ROOM_TYPE_ID, roomInfo.getChargingRoomTypeId());
		mContext.startActivity(intent);
	}

	/**
	 * 点击修改备注
	 * @param roomInfo  房间信息
	 */
	private void onRemarkClick(RoomInfo roomInfo)
	{
		Intent intent = new Intent(mContext, CustomDialogActivity.class);
		intent.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, "房间备注(" + roomInfo.getRoomNum() + ")");
		intent.putExtra(IntentKey.CUSTOM_DIALOG_TYPE, CustomDialogActivity.DialogEnum.REMARK.toString());
		intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, roomInfo.getRoomRemark());
		intent.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
		intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT_HINT, "输入房间备注内容");
		((CustomActivity) mContext).startActivityForResult(intent, IntentCode.ROOM_REMARK_REQUEST);
	}

	/**
	 * 点击修改人数
	 * @param roomInfo 房间信息
	 */
	private void onPeopleCountClick(RoomInfo roomInfo)
	{
		Intent intent1 = new Intent(mContext, SingleInputActivity.class);
		intent1.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, "修改人数(" + roomInfo.getRoomNum() + ")");
		intent1.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, String.valueOf(roomInfo.getRoomPeopleCount()));
		intent1.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
		intent1.putExtra(IntentKey.BTN_SURE, "确定");
		intent1.putExtra(IntentKey.BTN_CANCEL, "取消");
		intent1.putExtra(IntentKey.ACTIVITY_ENUM, SingleInputActivity.ActivityEnum.ROOM_NUMBERS);
		((CustomActivity) mContext).startActivityForResult(intent1, IntentCode.ROOM_PEOPLE_REQUEST);
	}

	/**
	 * 点击消费查询
	 * @param roomNum 房间号
	 * @param billId 账单id
	 */
	private void onConsumeQueryClick(String roomNum, String billId)
	{
		Intent intent = new Intent(mContext, ConsumeInfoActivity.class);
		intent.putExtra(IntentKey.ROOM_NUM, roomNum);
		intent.putExtra(IntentKey.BILL_ID, billId);
		mContext.startActivity(intent);
	}

}
